<template>
  <div>
    <!-- 搜索 -->
    <div class="search-form">
      <el-form :inline="true" :model="params" size="small">
        <el-form-item label="歌曲名称" label-width="80px">
          <el-input
            v-model="params.name"
            placeholder="请输入歌曲名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="启用状态" label-width="80px">
          <el-select v-model="params.enable" placeholder="启用状态" clearable>
            <el-option label="启用" :value="1" />
            <el-option label="未启用" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 搜索结束 -->

    <!-- 按钮 -->
    <div class="buttom-group">
      <el-button type="primary" size="mini" @click="toAdd">添加</el-button>
    </div>
    <!-- 按钮结束 -->

    <!-- 数据表格 -->
    <div class="data-table">
      <el-table
        :data="musicList"
        style="width: 100%"
        height="520"
        :header-cell-style="{ 'text-align': 'center' }"
        :cell-style="{ 'text-align': 'center' }"
      >
        <el-table-column prop="cover" label="封 面" width="150px">
          <template slot-scope="scope">
            <el-image
              style="width: 80px; height: 80px"
              :src="scope.row.cover"
              fit="fill"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="歌 曲 名 称" width="180px">
        </el-table-column>
        <el-table-column prop="artist" label="歌 手" width="120px">
        </el-table-column>
        <el-table-column prop="url" label="歌 曲 链 接" width="300px">
        </el-table-column>

        <el-table-column prop="enable" label="启 用" width="200px">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              @change="changeEnable(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column prop="createdTime" label="创 建 时 间">
        </el-table-column>

        <el-table-column label="操 作" width="150px" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="toUpdate(scope.row.id)"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="deleteById(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 数据表格结束 -->

    <!-- 添加弹窗 -->
    <el-dialog title="添加" :visible.sync="addDialog" width="50%">
      <music-add
        v-if="addDialog"
        @after="getByPage"
        @closeDialog="closeDialog"
      />
    </el-dialog>

    <!-- 添加弹出结束 -->

    <!-- 修改弹窗 -->
    <el-dialog title="编辑" :visible.sync="updateDialog" width="50%">
      <music-update
        v-if="updateDialog"
        :active-id="activeId"
        @after="getByPage"
        @closeDialog="closeDialog"
      />
    </el-dialog>
    <!-- 修改弹窗结束 -->

    <!-- 分页组件 -->
    <div class="pageable-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="params.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="count"
      >
      </el-pagination>
    </div>
    <!-- 分页组件结束 -->
  </div>
</template>

<script>
import musicAdd from "./music-add";
import musicUpdate from "./music-update";
import { getByPage, deleteById,updateEnable } from "@/api/music";

export default {
  components: {
    musicAdd,
    musicUpdate,
  },
  data() {
    return {
      // 音乐鉴赏列表
      musicList: [],
      // 分页参数
      params: {
        page: 1,
        limit: 10,
      },
      // 总条数
      count: 0,
      // 当前点击的ID
      activeId: null,
      // 控制弹窗显示
      addDialog: false,
      updateDialog: false,
    };
  },
  created() {
    this.getByPage();
  },
  methods: {
    // 当前页条数发生改变时触碰
    handleSizeChange(val) {
      this.params.limit = val;
      this.getByPage();
    },
    // 当前页发生改变时触碰
    handleCurrentChange(val) {
      this.params.page = val;
      this.getByPage();
    },
    // 音乐鉴赏列表
    getByPage() {
      getByPage(this.params).then((res) => {
        this.musicList = res.data;
        this.count = parseInt(res.count);
      });
    },
    // 查询
    search() {
      this.getByPage();
    },
    // 打开添加弹窗
    toAdd() {
      this.addDialog = true;
    },
    // 打开修改弹窗
    toUpdate(id) {
      this.updateDialog = true;
      this.activeId = id;
    },
    // 关闭弹窗
    closeDialog() {
      this.addDialog = false;
      this.updateDialog = false;
    },
    //删除
    deleteById(id) {
      this.$confirm("此操作将永久删除该音乐, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteById(id).then((res) => {
          this.$message.success(res.msg);
          this.getByPage();
        });
      });
    },
    // 启用禁用开关改变时触发
    changeEnable(data) {
      // data 里的值是改变后的值
      if (data.enable === 0) {
        this.$confirm("是否禁用该歌曲?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          updateEnable(data).then((res) => {
            this.$message.success(res.msg);
            this.getByPage();
          });
        }).catch(() => {
          data.enable = 1    // 取消后调用
        });
      } else {
        this.$confirm("是否启用该歌曲?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "success",
        }).then(() => {
          updateEnable(data).then((res) => {
            this.$message.success(res.msg);
            this.getByPage();
          });
        }).catch(() => {
          data.enable = 0
        });
      }
    },
  },
};
</script>

<style>
</style>